package cn.ghx.tool.novel.grabber;

import cn.ghx.tool.novel.domain.Book;
import cn.ghx.tool.novel.domain.Caption;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 抓取笔趣库的书籍
 * Created by Scylla on 16/4/27.
 */
public class GrabberBiqu extends GrabberBase {

    GrabberBiqu(String url,String path) {
        super(url,path);
    }

    @Override
    protected Book getBookWithoutContent(Document doc) {
        Book book = new Book();
        String bookTitle = doc.select("meta[property=og:novel:title]").get(0).attr("content");
        String author = doc.select("meta[property=og:novel:author]").get(0).attr("content");

        book.setTitle(bookTitle);
        book.setAuthor(author);

        Elements links = doc.select("#list dd a");
        if (links.size() > 0) {
            for (Element a : links){
                String caption = a.ownText();
                String link =  url + "/"+a.attr("href");
                Caption cap = new Caption(caption,link);
                book.getCaptions().add(cap);
            }

        }
        return book;
    }

    @Override
    public String getCaptionContent(String link) {
        try {
            Document doc = Jsoup.connect(link).get();
            Element el = doc.getElementById("content");
            return el.ownText().replace(" ","").replaceAll(" ","");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
